package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加商品进购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    @Transactional
    public synchronized void add(ShoppingCartDTO shoppingCartDTO) {

        Long userId = BaseContext.getCurrentId();
        if(shoppingCartDTO.getDishId() == null && shoppingCartDTO.getSetmealId() == null)
        {
            throw new ShoppingCartBusinessException(MessageConstant.UNKNOWN_ERROR);
        }

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        log.info("shoppingCart:{}", shoppingCart);
        //查看当前商品是否已经在购物车里了！！
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShopingCartByShoppingCart(shoppingCart);
        if(shoppingCartList != null && shoppingCartList.size() > 0)
        {
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateItemNum(shoppingCart);
            return;
        }

        if(shoppingCartDTO.getSetmealId() != null)
        {
            Setmeal setmeal = setmealMapper.getSetmealById(shoppingCartDTO.getSetmealId());
            BeanUtils.copyProperties(setmeal, shoppingCart);
            shoppingCart.setAmount(setmeal.getPrice());
        }
        else if(shoppingCartDTO.getDishId() != null)
        {
            DishVO dish = dishMapper.getDishById(shoppingCartDTO.getDishId());
            BeanUtils.copyProperties(dish, shoppingCart);
            shoppingCart.setAmount(dish.getPrice());
        }
        else throw new ShoppingCartBusinessException(MessageConstant.UNKNOWN_ERROR);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.add(shoppingCart);

    }

    /**
     * 查询购物车商品
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingCartList() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.getShopingCartByShoppingCart(shoppingCart);
    }

    /**
     * 删除购物车中的一样商品
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShopingCartByShoppingCart(shoppingCart);
        shoppingCart = shoppingCartList.get(0);
        Integer num = shoppingCart.getNumber();
        if(num > 1)
        {
            shoppingCart.setNumber(num - 1);
            shoppingCartMapper.updateItemNum(shoppingCart);
        }
        else if(num == 1)
        {
            shoppingCartMapper.delete(shoppingCart);
        }
        else throw new ShoppingCartBusinessException(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanAllItems() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        shoppingCartMapper.delete(shoppingCart);
    }
}
